# -*- coding: utf-8 -*-

'''

scrapy : error : Unable to find vcvarsall.bat
下载twisted
'''

# 写入文件
def write_to_file(file_name, txt):
    '''''
        讲txt文本存入到file_name文件中
    '''
    print("正在存储文件" + str(file_name));
    # 1 打开文件
    # w 如果没有这个文件将创建这个文件
    f = open(file_name, 'w', encoding='utf-8');
    # 2 读写文件
    f.write(str(txt));
    # 3 关闭文件
    f.close();


if __name__ == '__main__':

    file_object = open('D:\software\cz88.net\ip/ip_list.txt', 'r');
    txtLine =  0;

    text = "";


    try:
        for line in file_object:
            strAarr = line.split(" ", 100);
            #txtLine = txtLine + strAarr[0] + strAarr[len(strAarr) - 2] + "\r\n";
            if strAarr[0] != "" and strAarr[len(strAarr) - 2] != "" and "IANA保留地址" != strAarr[len(strAarr) - 2]:
             txtLine = txtLine + 1;
             #print(strAarr[0], strAarr[len(strAarr) - 2] );
             #print(txtLine)
             sql = "INSERT into dwc_ip_list (ip_address) VALUES ('"+strAarr[0]+ "'"+")" +";\n";
             text = text + sql;
             print(sql);
    finally:
        file_object.close();

        print(txtLine);
        write_to_file("D:\software\cz88.net\ip/ip_list2.sql",text);